package com.caihao.docdemo.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.Map;

/**
 * 获取freemarker模板字符串
 * @author lpf
 */
@Slf4j
public class FreeMarkUtil {

    public static Configuration getMyConfiguration() throws IOException {
        //创建配置实例
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
        //设置编码
        configuration.setDefaultEncoding("utf-8");
//        configuration.setClassForTemplateLoading(FreeMarkUtil.class, "templates/file/test2");
        configuration.setDirectoryForTemplateLoading(new File("D:\\accrate\\uploadPath\\test"));
        return configuration;
    }

    public static Configuration getConfiguration(String sourcePath) throws IOException {
        //创建配置实例
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
        //设置编码
        configuration.setDefaultEncoding("utf-8");
//        configuration.setClassForTemplateLoading(FreeMarkUtil.class, "templates/file/test2");
        configuration.setDirectoryForTemplateLoading(new File(sourcePath));
        return configuration;
    }

    /**
     * 获取模板字符串输入流
     * @param dataMap   参数
     * @param templateName  模板名称
     * @return
     */
    public static ByteArrayInputStream getFreemarkerContentInputStream(String sourcePath, Map dataMap, String templateName) {
        ByteArrayInputStream in = null;
        try {
            //获取模板
            Template template = getConfiguration(sourcePath).getTemplate(templateName);
//            Template template = new Template("D:\\workspace\\mavenprojectdemo\\test-web-demo\\src\\main\\resources" +
//                    "\\static\\file\\test2\\" + templateName, new InputStreamReader(new FileInputStream(new File
//                    ("D:\\workspace\\mavenprojectdemo\\test-web-demo\\src\\main\\resources\\static\\file\\test2\\" +
//                            templateName))), getMyConfiguration());
            StringWriter swriter = new StringWriter();
            //生成文件
            template.process(dataMap, swriter);
            in = new ByteArrayInputStream(swriter.toString().getBytes("utf-8"));//这里一定要设置utf-8编码 否则导出的word中中文会是乱码
        } catch (Exception e) {
            log.error("模板生成错误！");
            e.printStackTrace();
        }
        return in;
    }

    public static byte[] getFreemarkerBytes(Map dataMap, String templateName) {
        ByteArrayInputStream in = null;
        try {
            //获取模板
//            Template template = getConfiguration().getTemplate(templateName);
            Template template = new Template("G:\\workspace\\maven-demo1\\doc-demo\\src\\main\\resources\\static" +
                    "\\file\\test2\\"+templateName,new InputStreamReader(new FileInputStream(new File("G:\\workspace" +
                    "\\maven-demo1\\doc-demo\\src\\main\\resources\\static\\file\\test2\\"+templateName))),getMyConfiguration());
            StringWriter swriter = new StringWriter();
            //生成文件
            template.process(dataMap, swriter);
            return swriter.toString().getBytes("utf-8");//这里一定要设置utf-8编码 否则导出的word中中文会是乱码
        } catch (Exception e) {
            log.error("模板生成错误！");
            e.printStackTrace();
        }
        return null;
    }

}